<template>
    <el-dialog title="提交审核" :visible.sync="open" width="400px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="申请原因" prop="reason">
          <el-input v-model="form.reason" placeholder="请输入原因" />
        </el-form-item>
        <el-form-item label="审核人" prop="userIds">
          <el-select v-model="form.userIds" placeholder="请选择审核人" clearable multiple>
          <el-option
            v-for="u in userOptions"
            :key="u.value"
            :label="u.label"
            :value="u.value"
          />
        </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
</template>

<script>
import { restGet, apiPost, apiPatch, apiDs, apiDownload } from "@/api/openapi";

export default {
  name: "ApplyDialog",
  props:{
    //数据模型
      model: String,
    //数据主键
      applyData: Object
  },
  data() {
    return {
      open: false,
      userOptions: [],
      form:{},
      // 表单校验
      rules: {
        reason: [
          { required: true, message: "申请原因不能为空", trigger: "blur" }
        ],
        userIds: [
          { required: true, message: "审核人不能为空", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    apiDs('user_select').then(response=>{
        this.userOptions = response.data;
    });
  },
  watch: {
    applyData: function(newVal, oldVal){
      this.reset();
      if(newVal == null){
        this.open = false;
      }else{
        this.open = true;
      }
    }
  },
  methods: {
    reset() {
      this.form = {
        reason: this.applyData == null ? null: this.applyData.reason,
        userIds: [],
      }
    },
    /** 取消按钮操作 */
    cancel() {
      this.$emit('cancel');
    },
    // 提交上传文件
    submit() { 
      this.$refs["form"].validate(valid => {
        if (valid) {
            let data = {
              'flow_task': {
                type: this.model,
                target_id: this.applyData['id'],
                num: this.applyData['num'],
                title: this.applyData['title'],
                user_id:'@userId()',
                user_name:'@userName()',
                apply_time:'@now()',
                check_status:'1',
                apply_reason: this.form.reason
              },
              'flow_step':[],
              "@tags": ['flow_task', 'flow_step']
            };
            for(var i=0;i<this.form.userIds.length;i++){
              let userName = null;
              for(var j=0;j<this.userOptions.length;j++){
                if(this.userOptions[j].value == this.form.userIds[i]){
                  userName = this.userOptions[j].label;
                }
              }
              data['flow_step'].push({
                'task_id': "@flow_task.id",
                'check_user_id': this.form.userIds[i],
                'check_user_name': userName,
                'check_status':'1',
                'pid':0
              })
            }
         
            apiPatch(data).then(response => {
              this.$modal.msgSuccess("提交成功");
              this.$emit('success');
            });
        }
      });
    }
  }
};
</script>
